package controllers;

import java.io.File;
import java.io.FileReader;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.util.Collection;
import java.util.Stack;

import models.Activity;
import models.User;
import utils.Serializer;
import utils.XMLSerializer;
import asg.cliche.Command;
import asg.cliche.Param;
import asg.cliche.Shell;
import asg.cliche.ShellFactory;

import com.google.common.base.Optional;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class Main
{
	
	
	PacemakerAPI paceApi;
	public Main() throws Exception
	  {
	    File  datastore = new File("datastore.xml");
	    Serializer serializer = new XMLSerializer(datastore);

	    paceApi = new PacemakerAPI(serializer);
	    if (datastore.isFile())
	    {
	      paceApi.load();
	    }
	  }
	
  @Command(description="Create a new User")
  public void createUser (@Param(name="first name") String firstName, @Param(name="last name") String lastName, 
                          @Param(name="email")      String email,     @Param(name="password")  String password)
  {
    paceApi.createUser(firstName, lastName, email, password);
  }
  
  @Command(description="Create a new User")
  public void listUser (@Param(name="email") String email)
  {
    User user = paceApi.getUser(email);
    System.out.println(user);
  }
  
  @Command(description="Create a new User")
  public void listUsers ()
  {
    Collection<User> users = paceApi.getUsers();
    System.out.println(users);
  }
  
  @Command(description="Create a new User")
  public void listIdUserS(@Param(name="id") Long id){
	  User user = paceApi.getUser(id);
	    System.out.println(user);
	  
  }
  
  @Command(description="Create a new User")
  public void listActivities (@Param(name="id") Long id)
  {
    Activity activities = paceApi.getActivity(id);
    System.out.println(activities);
  }
  
  @Command(description="Delete a User")
  public void deleteUser (@Param(name="email") String email)
  {
    paceApi.deleteUser(email);
  }
  
  @Command(description="Delete a User by ID")
  public void deleteUser (@Param(name="id") Long id)
  {
    paceApi.deleteUser(id);
  }
  
//  @Command(description="Change File Format")
//  public void changeFileFormat (@Param(name="fileOrg") String fileOrg, @Param(name="fileChange") String fileChange) throws Exception
//  {
////    if (fileOrg.equals("xml") && fileChange.equals("json")){
////       
////	   File  datastore = new File("datastore.xml");
////	   InputStream in = ConvertXMLtoJSON.class.getResourceAsStream(datastore);
////	   String xml = IOUtils.toString(in);
////
////	   XMLSerializer xmlSerializer = new XMLSerializer(datastore); 
////	   JSON json = xmlSerializer.read();  
////	   System.out.println(json.toString(2));
////    }
//	  
//	  Stack stack = new Stack();
//	  ObjectInputStream is = null;
//	  try{
//		  XStream xstream = new XStream(new DomDriver());
//	      is = xstream.createObjectInputStream(new FileReader(new File("datastore.xml")));
//	      Object obj = is.readObject();
//	      while (obj != null)
//	      {
//	        stack.push(obj);
//	        User usr = (User)is.readObject();
//	        XStream jsTream = new XStream(new JettisonMappedXmlDriver());
//	        String strJSON = jsTream.toXML(usr);
//	      }
//	  }
//	  finally
//	  {
//		  
//	  }
//  }
  
  @Command(description="Add an activity")
  public void addActivity (@Param(name="user-id")  Long   id,       @Param(name="type") String type, 
                           @Param(name="location") String location, @Param(name="distance") double distance)
  {
    Optional<User> user = Optional.fromNullable(paceApi.getUser(id));
    if (user.isPresent())
    {
      paceApi.createActivity(id, type, location, distance);
    }else{
    	System.out.println("Can't find user with id = " + id);
    }
  }

  @Command(description="Add Location to an activity")
  public void addLocation (@Param(name="activity-id")  Long  id,   
                           @Param(name="latitude")     float latitude, @Param(name="longitude") float longitude)
  {
    Optional<Activity> activity = Optional.fromNullable(paceApi.getActivity(id));
    if (activity.isPresent())
    {
      paceApi.addLocation(activity.get().id, latitude, longitude);
    }else{
    	System.out.println("Can't find Activity with id = " + id);
    }
  }
  
  public static void main(String[] args) throws Exception
  {
    Main main = new Main();

    Shell shell = ShellFactory.createConsoleShell("pm", "Welcome to pacemaker-console - ?help for instructions", main);
    shell.commandLoop();

    main.paceApi.store();
  }
  
  @Command(description="create XML")
  public void store(){
	  try {
		paceApi.store();
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
  }
  
  @Command(description="load xml file")
  public void load(){
	  try {
		paceApi.load();
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
  }
  
  @Command(description="Change format file")
  public void changeFileFormat(@Param(name="Source file extendsion") String sourceFileExtendSion
		  , @Param(name="target file extendsion") String targetFileExt) throws Exception{
	  paceApi.changeFormatFile(sourceFileExtendSion, targetFileExt);
  }
  
}
